package control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2.ejb.entity.Capacita;
import swimv2.ejb.entity.Lavoro;
import swimv2.ejb.remote.GestioneCapacitaRemote;
import swimv2.ejb.remote.GestioneLavoroRemote;
import swimv2.ejb.util.LoginToken;
import util.HtmlUtil;

/**
 * Servlet implementation class Lavoro
 */
public class Lavori extends Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Lavori() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String azione=(String) request.getParameter("azione");
		if(azione == null)
		{
			showElencoLavori(request,response);
		}else
		if(azione.equals("creaLavoro"))
		{
			showCreaLavoro(request,response);
		}

	}

	private void showElencoLavori(HttpServletRequest request,HttpServletResponse response) {
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
		String username=tok.getUsername();

			try {
	        	Context jndiContext  = new javax.naming.InitialContext();
				Object ref1 = jndiContext.lookup("GestioneLavoro/remote");
				GestioneLavoroRemote gestioneLavoroRemote =(GestioneLavoroRemote) PortableRemoteObject.narrow(ref1, GestioneLavoroRemote.class);
				
				request.setAttribute("daValutare", gestioneLavoroRemote.getLavoriDaValutareDatore(username));
				request.setAttribute("inOfferta", gestioneLavoroRemote.getLavoriInOffertaDatore(username));
				request.setAttribute("lavoriConclusi", gestioneLavoroRemote.getLavoriConclusiDatore(username));
				request.setAttribute("inCorso", gestioneLavoroRemote.getLavoriInCorsoDatore(username));
				
				HtmlUtil.CompletaEdIncludi(request, response, "/WEB-INF/pages/elencoLavori.jsp");
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		
	}

	private void showCreaLavoro(HttpServletRequest request, HttpServletResponse response) {
		
		
		try {
        	Context jndiContext  = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
			List<Capacita> capacita = gestioneCapacitaRemote.getListaCapacita();
			request.setAttribute("capacita", capacita);
						
			HtmlUtil.CompletaEdIncludi(request, response, "/WEB-INF/pages/creaLavoro.jsp");
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String azione=(String) request.getParameter("azione");
		if(azione.equals("creaLavoro"))
		{
			creaLavoro(request,response);
			
			
		} else if(azione.equals("accettaDipendente"))
		{
			accettaDipendente(request,response);	
		}else if(azione.equals("paginaCommento"))
		{
			showCommentaLavoro(request,response);	
		}
		else if(azione.equals("commentaLavoro"))
		{
			commentaLavoro(request,response);	
		}
		
		
			
			
		
		
		
	}


	private void commentaLavoro(HttpServletRequest request,
			HttpServletResponse response) {
		try {
        	Context jndiContext  = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneLavoro/remote");
			GestioneLavoroRemote gestioneLavoroRemote =(GestioneLavoroRemote) PortableRemoteObject.narrow(ref1, GestioneLavoroRemote.class);
			int idLavoro = Integer.parseInt(request.getParameter("id"));
			String commento = request.getParameter("commento");
			gestioneLavoroRemote.commentaLavoro(commento, idLavoro);
			
			HtmlUtil.redirect(request, response, "/Lavori");
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void showCommentaLavoro(HttpServletRequest request,
			HttpServletResponse response) {
		try {
        	Context jndiContext  = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneLavoro/remote");
			GestioneLavoroRemote gestioneLavoroRemote =(GestioneLavoroRemote) PortableRemoteObject.narrow(ref1, GestioneLavoroRemote.class);
			int id = Integer.parseInt(request.getParameter("id"));
			request.setAttribute("lavoro", gestioneLavoroRemote.findLavoro(id));
			
			
			HtmlUtil.CompletaEdIncludi(request, response, "/WEB-INF/pages/commentaLavoro.jsp");
			
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void accettaDipendente(HttpServletRequest request,
			HttpServletResponse response) {
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
		String datore=tok.getUsername();
		int idLavoro = Integer.parseInt(request.getParameter("idLavoro"));
		String dipendente = request.getParameter("idDipendente");
		
		try {
        	Context jndiContext  = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneLavoro/remote");
			GestioneLavoroRemote gestioneLavoroRemote =(GestioneLavoroRemote) PortableRemoteObject.narrow(ref1, GestioneLavoroRemote.class);
			gestioneLavoroRemote.accettaCandidato(datore, idLavoro, dipendente);
						
			HtmlUtil.redirect(request, response, "/Lavori");
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		

		
	}

	private void creaLavoro(HttpServletRequest request,HttpServletResponse response) {
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
		String username=tok.getUsername();
		String azione=(String) request.getParameter("azione");
		if(azione.equals("creaLavoro"))
		{
			String[] lista = request.getParameterValues("CapacitaRichieste");
			List<Integer> elencoRequisiti= new ArrayList<Integer>();
			
			if(lista != null)
				for(String s:lista)
				{
					elencoRequisiti.add(Integer.parseInt(s));
				}
			String descBreve = request.getParameter("descrizioneBreve");
			String descEstesa = request.getParameter("descrizioneEstesa");
			boolean soloAmici  = (request.getParameter("soloAmici") != null);
			try {
	        	Context jndiContext  = new javax.naming.InitialContext();
				Object ref1 = jndiContext.lookup("GestioneLavoro/remote");
				GestioneLavoroRemote gestioneLavoroRemote =(GestioneLavoroRemote) PortableRemoteObject.narrow(ref1, GestioneLavoroRemote.class);
				
				gestioneLavoroRemote.creaLavoro(username, descBreve, descEstesa, new TreeSet<Integer>(elencoRequisiti),soloAmici);
							
				HtmlUtil.redirect(request, response, "/Lavori");
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
			
			
			
			
		}		
	}

}
